#include "BaseObject.h"

BaseObject::BaseObject()
			: m_v3Pos(0.0f, 0.0f, 0.0f)
			, m_v3Rot(0.0f, 0.0f, 0.0f)
			, m_v3Scale(1.0f, 1.0f, 1.0f)
			, objectType(ERROR_TYPE)
			, active(true)
{}

BaseObject::BaseObject(float x, float y, float z)
			: objectType(ERROR_TYPE)
			, m_v3Pos(x, y, z) 
			, active(true)
			, m_v3Rot(0, 0, 0) 
			, m_v3Scale(1, 1, 1)    
{}

BaseObject::BaseObject(Vector3D position, Vector3D rotation, Vector3D scale)
			: objectType(ERROR_TYPE)
			, active(true)
			, m_v3Pos(position)
			, m_v3Rot(rotation)
			, m_v3Scale(scale)
{}

BaseObject::BaseObject(float x, float y)
			: m_v3Pos(x, y, 0.0f)
			, active(true)
			, objectType(ERROR_TYPE) 
{}

void BaseObject::set(BaseObject b)
{
	m_v3Pos = b.m_v3Pos;
	m_v3Rot = b.m_v3Rot;
	m_v3Scale = b.m_v3Scale;
	objectType = b.objectType;
	assetIndex = b.assetIndex;
	active = b.active;
}

void BaseObject::serialize(ByteBuffer* & info)
{
	info->addFloat(m_v3Pos.x);
	info->addFloat(m_v3Pos.y);
	info->addFloat(m_v3Pos.z);

	info->addFloat(m_v3Rot.x);
	info->addFloat(m_v3Rot.y);
	info->addFloat(m_v3Rot.z);

	info->addFloat(m_v3Scale.x);
	info->addFloat(m_v3Scale.y);
	info->addFloat(m_v3Scale.z);

	info->addLong(objectType);
	info->addLong((long)assetIndex.x);
	info->addLong((long)assetIndex.y);

	if(active)
		info->addByte((char)true);
	else
		info->addByte((char)false);

}

void BaseObject::deserialize(ByteBuffer* info, int & index)
{
	if(!info)
		return;

	const int flSize = sizeof(long) + sizeof(short);
	float x, y, z;

	// retrieve the object's position
	x = info->getFloat(index); index += flSize;
	y = info->getFloat(index); index += flSize;
	z = info->getFloat(index); index += flSize;
	SetPos(x, y, z);

	// retrieve the object's rotation
	x = info->getFloat(index); index += flSize;
	y = info->getFloat(index); index += flSize;
	z = info->getFloat(index); index += flSize;
	setRotation(x, y, z);

	// retrieve the object's scale
	x = info->getFloat(index); index += flSize;
	y = info->getFloat(index); index += flSize;
	z = info->getFloat(index); index += flSize;
	setScale(x, y, z);

	objectType = (e_objectType)info->getLong(index); index += sizeof(long);
	assetIndex.x = (float)info->getLong(index); index += sizeof(long);
	assetIndex.y = (float)info->getLong(index); index += sizeof(long);

	if(info->getByte(index))
		active = true;
	else
		active = false;
	
	index += 1;
}